package  test;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class AppCfg {

    public  static String sDbPort, sDbUser, sDbPwd,
            sDbHost,sUploadPath,sDbName;

    public static void init() {

        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            DefaultHandler handler = new DefaultHandler() {

                boolean bUploadPath = false
                        , bDbPwd = false
                        , bDbHost = false
                        , bDbUser = false
                        , bDbPort = false
                        , bDbName;

                public void startElement(String uri, String localName,String qName,
                                         Attributes attributes) throws SAXException {

                    //System.out.println("Start Element :" + qName);

                    if (qName.equalsIgnoreCase("uploadPath")) {
                        bUploadPath = true;
                    }

                    if (qName.equalsIgnoreCase("dbHost")) {
                        bDbHost = true;
                    }

                    if (qName.equalsIgnoreCase("dbUser")) {
                        bDbUser = true;
                    }
                    if (qName.equalsIgnoreCase("dbPwd")) {
                        bDbPwd = true;
                    }
                    if (qName.equalsIgnoreCase("dbPort")) {
                        bDbPort = true;
                    }
                    if (qName.equalsIgnoreCase("dbName")) {
                        bDbName = true;
                    }



                }

                public void endElement(String uri, String localName,
                                       String qName) throws SAXException {

                    //System.out.println("End Element :" + qName);

                }

                public void characters(char ch[], int start, int length) throws SAXException {

                    if (bUploadPath) {
                        sUploadPath = new String(ch, start, length);
                        bUploadPath = false;
                    }

                    if (bDbUser) {
                        sDbUser = new String(ch, start, length);
                        bDbUser = false;
                    }

                    if (bDbPwd) {
                        sDbPwd = new String(ch, start, length);
                        bDbPwd = false;
                    }
                    if (bDbPort) {
                        sDbPort = new String(ch, start, length);
                        bDbPort = false;
                    }
                    if (bDbHost) {
                        sDbHost = new String(ch, start, length);
                        bDbHost = false;
                    }
                    if (bDbName) {
                        sDbName = new String(ch, start, length);
                        bDbName = false;
                    }


                }

            };

            saxParser.parse(System.getProperty("user.dir")+"/config.xml", handler);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}